/**
 *
 * @description 全局守卫权限路由控制
 */

import router from '@/router'
import store from '@/store'
import {
    getInfo
} from '@/api/user'
import {
    White,
    redirectName,
    _intercept,
} from '@/utils/setting'

router.beforeEach(async (to, from, next) => {
    if (_intercept.includes(to.name)) {
        store.commit('config/changeShow', true)
    } else {
        store.commit('config/changeShow', false)
    }
    if (White.includes(to.name) || to.path.indexOf('/register') != -1) {
        next()
    } else {
        const userToken = store.state.user.token || localStorage.getItem('token');
        if (userToken && userToken != 'undefined') {
            next()
            await getInfo().then(userRes => {
                store.commit('user/setUserInfo', userRes.datas)
            })
        } else {
            next({
                name: redirectName
            })
        }
    }

})